package controllers

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"goblog/models"
	"strconv"
)

func GetUsers(c *gin.Context)  {
	users := models.GetUsers()
	c.JSON(http.StatusOK,gin.H{
		"code" : http.StatusOK,
		"data" : users,
	})
}

func GetPageUsers(c *gin.Context)  {
	pageNum := c.Query("pageNum")
	pageSize := c.Query("pageSize")

	var pSize int = 10
	if pageSize != "" {
		pSize, _ = strconv.Atoi(pageSize)
	}

	var pNum int = 1
	if pageNum != "" {
		pNum, _= strconv.Atoi(pageNum)
	}

	users := models.GetPageUsers(pNum,pSize)
	if users != nil {
		c.JSON(http.StatusOK,models.ResS(users))
		//c.JSON(http.StatusOK,gin.H{
		//	"code" : http.StatusOK,
		//	"data" : users,
		//})
	}

}

func GetUserById(c *gin.Context)  {
	id := c.Param("id")
	if id == "" {
		c.JSON(http.StatusOK,models.ResE("getuser is error"))
	}else {
		user:= models.GetUserById(id)

		c.JSON(http.StatusOK,gin.H{
			"code" : http.StatusOK,
			"data" : user,
		})

	}
}